package cn.manter.org.service.impl;

import cn.manter.basic.dto.SmsCodeBto;
import cn.manter.basic.exception.BusinessException;
import cn.manter.basic.utils.BaiduAuditUtils;
import cn.manter.org.domain.Employee;
import cn.manter.org.domain.Shop;
import cn.manter.org.domain.ShopAuditLog;
import cn.manter.org.mapper.EmployeeMapper;
import cn.manter.org.mapper.ShopMapper;
import cn.manter.org.service.IEmployeeService;
import cn.manter.org.service.IShopAuditLogService;
import cn.manter.org.service.IShopService;
import cn.manter.basic.service.impl.BaseServiceImpl;
import cn.manter.org.vo.ShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Mr.Zhao
 * @since 2022-07-01
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

   /* @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;*/

    @Autowired
    private IShopAuditLogService shopAuditLogService;

    @Autowired
    private IEmployeeService employeeService;

    // 注入邮件对象
    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public void settlement(Shop shop) {
        Employee admin = shop.getAdmin();
        // 1.判断传入参数
        if (StringUtils.isEmpty(shop.getName())){
            throw new BusinessException("店铺名称不能为空，请重新输入！");
        }
        // 修改状态
        // 判断手机号和邮箱是否真实存在


        // 2.判断两次密码是否一致
        if (!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BusinessException("两次密码不一致，请重新输入！");
        }

        // 判断店铺名是否合规
        Boolean censor = BaiduAuditUtils.TextCensor(shop.getName());
        if(!censor){
            throw new BusinessException("请输入合法信息！");
        }
        Shop loadShop = shopMapper.loadByName(shop.getName());
        // 3.判断店铺是否已经存在
        if (loadShop != null){
            throw new BusinessException("店铺名已注册，请重新输入");
        }

        // 4.判断管理员是否已被注册，并且绑定了店铺
        Employee loadEmp =employeeMapper.loadByUsername(admin.getUsername());
        
        if(loadEmp != null && loadEmp.getShopId() != null){
            throw new BusinessException("管理员已经绑定了店铺，请重新输入");
        }


        // 5.判断管理员是否被注册，没有注册就先保存管理员
        if(loadEmp == null){
            // 添加到数据库
            employeeService.save(admin);
        }
        // 1.判断手机号不为空
        if(StringUtils.isEmpty(admin.getPhone())){
            throw new BusinessException("手机号不能为空");
        }

        // 3.判断手机验证码不为空
        SmsCodeBto smsCodeBto = new SmsCodeBto();
        if(StringUtils.isEmpty(smsCodeBto.getPhoneCodeValue())){
            throw new BusinessException("手机验证码不能为空");
        }
        // 4.判断密码验证码不为空
        if(StringUtils.isEmpty(admin.getPassword())){
            throw new BusinessException("密码不能为空");
        }
        // 6.设置管理员的信息到店铺的admin字段中,并进行保存
        shop.setAdminId(admin.getId());
        // 添加到店铺字段中
        shopMapper.save(shop);

        //.将店铺的主键设置到管理员的外键中
        admin.setShopId(shop.getId());
        employeeService.update(admin);
    }
    // 驳回的实现类
    @Override
    public void reject(ShopAuditLog shopAuditLog){

        // 设置固定的店铺管理员的店铺id
        shopAuditLog.setAuditId(327);

        Shop shop = shopMapper.loadById(shopAuditLog.getShopId().longValue());
        shop.setState(-1);
        shopMapper.update(shop);

        // 驳回
        shopAuditLogService.save(shopAuditLog);
        // 2.发送复杂邮件的工具类
        try{
            // 1.创建复杂邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            // 2.发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("312717610@qq.com");
            helper.setSubject("宠物之间店铺入驻结果通知！");
            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>亲，你的店铺入驻审核已完成，但因为你的店铺名不合规，被驳回，请点击重试</h1>"+
                    "店铺信息修改：<a href='http://localhost:8081/#/Register?shopId="+shopAuditLog.getShopId()+"'>点我有惊喜</a>",true);
            // 5.设置收件人
            helper.setTo("312717610@qq.com");
            // 6.发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
    // 通过的实现类
    @Override
    public void pass(ShopAuditLog shopAuditLog) {
        // 设置固定的店铺管理员的店铺id
        shopAuditLog.setAuditId(327);

        Shop shop = shopMapper.loadById(shopAuditLog.getShopId().longValue());
        shop.setState(2);
        shopMapper.update(shop);


        shopAuditLogService.save(shopAuditLog);
        // 2.发送复杂邮件的工具类
        try{
            // 1.创建复杂邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            // 2.发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("312717610@qq.com");
            helper.setSubject("宠物之间店铺入驻结果通知！");
            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>亲，你的店铺入驻审核已成功，请点击激活！</h1>"+
                    "店铺激活链接：<a href='http://localhost:8080/shop/"+shopAuditLog.getShopId()+"'>点我有惊喜</a>",true);
            // 5.设置收件人
            helper.setTo("312717610@qq.com");
            // 6.发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Map<String, Object> loadEcharts() {
        // 得到报表数据
        List<ShopVo> shopVos = shopMapper.loadEcharts();
        // 将状态数据转化到list集合中
        /*List<Integer> states = shopVos.stream().map(ShopVo::getState).collect(Collectors.toList());*/
        List<Integer> counts = shopVos.stream().map(ShopVo::getCount).collect(Collectors.toList());
        List<String> states = new ArrayList<>();

        for (ShopVo shop:shopVos) {
            // 店铺状态：0：待审核、1：已审核、-1：驳回、2：待激活
            if(shop.getState() == 0){
                states.add("待审核");
            } else if(shop.getState() == 1){
                states.add("已审核");
            } else if(shop.getState() == 2){
                states.add("待激活");
            } else {
                states.add("驳回");
            }
        }
        HashMap<String, Object> map = new HashMap<>();

        map.put("x",states);
        map.put("y",counts);
        return map;
    }
    // 批量添加
    @Override
    public void batchAdd(List<Shop> shops) {
        shopMapper.batchAdd(shops);
    }
    // 拒绝
    @Override
    public void refuse(ShopAuditLog shopAuditLog) {
        // 设置固定的店铺管理员的店铺id
        shopAuditLog.setAuditId(327);

        Shop shop = shopMapper.loadById(shopAuditLog.getShopId().longValue());
        shop.setState(-1);
        shopMapper.update(shop);

        shopAuditLogService.save(shopAuditLog);
        // 2.发送复杂邮件的工具类
        try{
            // 1.创建复杂邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            // 2.发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("312717610@qq.com");
            helper.setSubject("宠物之间店铺入驻结果通知！");
            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>你的信息不真实，被拒绝！</h1>",true);
            // 5.设置收件人
            helper.setTo("312717610@qq.com");
            // 6.发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
